package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.PermDao;
import com.woniuxy.entity.Perm;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;

/**
 * @ClassName RbacPermServiceImpl
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/9/3 15:11
 * @Version 1.0
 */
@Service
public class RbacPermServiceImpl implements RbacPermService {
    @Autowired
    private PermDao permDao;

    public PermDao getPermDao() {
        return permDao;
    }

    public void setPermDao(PermDao permDao) {
        this.permDao = permDao;
    }

    @Override
    public ResponseEntity selectAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Perm> list= permDao.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "成功", pageInfo );
    }

    @Override
    @Transactional
    public ResponseEntity addPerm(Perm perm) {
        int row = permDao.addPerm(perm);
        if(row==0){
            return new ResponseEntity(500,"失败",null);
        }else {
            return new ResponseEntity(200, "成功", null);
        }
    }

    @Override
    public ResponseEntity deletePermById(int id) {
        int row = permDao.deletePermById(id);
        if(row==0){
            return new ResponseEntity(500,"失败",null);
        }else {
            return new ResponseEntity(200, "成功", null);
        }
    }

    @Override
    public List<Perm> findPermByName(String name) {
        List<Perm> list = permDao.findPermByName(name);
        return list;
    }

    @Override
    public ResponseEntity editOne(Perm perm) {
        if (perm.getId()!=0){
            int count = permDao.findCountByNameOrId(perm);
            if(count>1){
                return new ResponseEntity(500,"修改失败,账号同名！",null);
            }else{
                int i = permDao.updatePerm(perm);
                if (i>0){
                    return new ResponseEntity(200,"成功修改",null);
                }else {
                    return new ResponseEntity(500,"修改失败",null);
                }
            }
        }else{
            return new ResponseEntity(500,"id不能没有！",null);
        }
    }

    @Override
    public ResponseEntity search(int currentPage, int pageSize,String name) {
        PageHelper.startPage(currentPage,pageSize);
        List<Perm> list= permDao.search(name);
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "", pageInfo);
    }

    @Override
    public Set<String> findPermBySetRoles(String username) {
        return permDao.findPermBySetRoles(username);
    }
}
